package com.rc.saas.tenant.service.parking;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.utils.MD5Utils;
import com.rc.saas.tenant.mapper.parking.ElecParkingOrderMapper;
import com.rc.saas.tenant.model.parking.TenantParkingSupplier;
import com.rc.saas.tenant.search.parking.ElecParkingOrderSearch;
import com.rc.saas.tenant.vo.parking.ElecParkingOrderExportVo;
import com.rc.saas.tenant.vo.parking.ElecParkingOrderVo;
import com.rc.saas.tenant.vo.parking.TJDUnbindInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ElecParkingOrderServiceImpl implements ElecParkingOrderService {

    @Autowired
    private static Logger logger = LoggerFactory.getLogger(ElecParkingOrderServiceImpl.class);


    @Autowired
    private ElecParkingOrderMapper elecParkingOrderMapper;

    @Override
    public ElecParkingOrderMapper getMapper() {
        return elecParkingOrderMapper;
    }

    @Override
    public PageInfo<ElecParkingOrderVo> listPageParkingOrder(ElecParkingOrderSearch elecParkingOrderSearch) {
        PageHelper.startPage(elecParkingOrderSearch.getOffset(), elecParkingOrderSearch.getLimit());
        List<ElecParkingOrderVo> elecParkingOrderVoList = elecParkingOrderMapper.listPageParkingOrder(elecParkingOrderSearch);
        return new PageInfo<ElecParkingOrderVo>(elecParkingOrderVoList);
    }

    @Override
    public TJDUnbindInfo assembleTJDParkOrderInfoReq(String outTradeNum, String outTicketNum, TenantParkingSupplier tenantParkingSupplier) {
        String sigSecret = tenantParkingSupplier.getSigSecret();
        TJDUnbindInfo tjdUnbindInfo = new TJDUnbindInfo();
        tjdUnbindInfo.setService(tenantParkingSupplier.getService());
        tjdUnbindInfo.setVersion("1.0");
        tjdUnbindInfo.setPartner(tenantParkingSupplier.getPartner());
        tjdUnbindInfo.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        tjdUnbindInfo.setCharset("utf-8");
        tjdUnbindInfo.setSignType("md5");
        tjdUnbindInfo.setTradeId(outTradeNum);
        tjdUnbindInfo.setTicketId(outTicketNum);
        String sign = getSign(tjdUnbindInfo, sigSecret);
        tjdUnbindInfo.setSign(sign);
        return tjdUnbindInfo;
    }

    private String getSign(TJDUnbindInfo tjdUnbindInfo, String sigSecret) {
        //空值，signType 不参与签名
        StringBuilder signStr = new StringBuilder(
                "charset=" + tjdUnbindInfo.getCharset() +
                        "&partner=" + tjdUnbindInfo.getPartner() +
                        "&service=" + tjdUnbindInfo.getService() +
                        "&ticketId=" + tjdUnbindInfo.getTicketId() +
                        "&timestamp=" + tjdUnbindInfo.getTimestamp() +
                        "&tradeId=" + tjdUnbindInfo.getTradeId() +
                        "&version=" +
                        tjdUnbindInfo.getVersion() + sigSecret);
        System.out.println("signStr:" + signStr);
        logger.info("TJD签名串:{}", signStr);
        String sign = MD5Utils.getMD5Code(signStr.toString());

        System.out.println("sign:" + sign);
        return sign;

    }

    @Override
    public List<ElecParkingOrderExportVo> listElecParkingOrder(ElecParkingOrderSearch elecParkingOrderSearch) {
        return elecParkingOrderMapper.listElecParkingOrder(elecParkingOrderSearch);
    }
}
